package com.woyou.ui.fragment;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.UiThread;

import com.citaq.ideliver.R;
import com.woyou.bean.FmInfoBean;
import com.woyou.bean.Info;
import com.woyou.model.ActivitiesModel;
import com.woyou.model.ActivitiesModel_;
import com.woyou.model.FileModel;
import com.woyou.model.FileModel_;
import com.woyou.model.GoodsModel;
import com.woyou.model.GoodsModel_;
import com.woyou.model.OrderModel;
import com.woyou.model.OrderModel_;
import com.woyou.model.PropPanelModel;
import com.woyou.model.PropPanelModel_;
import com.woyou.model.ShoppingCarModel;
import com.woyou.model.ShoppingCarModel_;
import com.woyou.model.ShopsModel;
import com.woyou.model.ShopsModel_;
import com.woyou.model.UserModel;
import com.woyou.model.UserModel_;
import com.woyou.service.NetWorkCenter;
import com.woyou.service.NetWorkCenter_;
import com.woyou.service.ThreadPoolManager;
import com.woyou.service.ThreadPoolManager_;
import com.woyou.service.location.BaiduLocationService;
import com.woyou.service.location.BaiduLocationService_;
import com.woyou.ui.api.PwdErrorListener;
import com.woyou.ui.api.RetryNetwork;
import com.woyou.utils.Task;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.ImageView;
import android.widget.Toast;

/**
 * 
 * @author longtao.li
 * @param <T>
 * 
 */
@EFragment
public abstract class SuperFragment extends Fragment implements RetryNetwork,
		PwdErrorListener, OnTouchListener {
	
	ThreadPoolManager mThreadPoolManager;

	protected final String INFO_NAME = "info";
	
	@SuppressWarnings("rawtypes")
	protected FmInfoBean infoBean; // 接受跳转后要传递的数据bean
	
	protected Context mContext;

	protected Activity mActivity;
	
	protected BaiduLocationService mBaiduLocationService;

	protected NetWorkCenter mNetWorkCenter;;
	
	protected ProgressDialog progress, locationProgress;;

	/**所有Model**/
	protected ShopsModel mShopsModel;
	protected ShoppingCarModel mShoppingCarModel;
	protected GoodsModel mGoodsModel;
	protected ActivitiesModel mActivitiesModel;
	protected FileModel mFileModel;
	protected PropPanelModel mPropPanelModel;
	protected OrderModel mOrderModel;
	protected UserModel mUserModel;
	
	/**
	 * 动画
	 */
	private AnimationDrawable animationDrawable;


	/**
	 * 当前最后执行的线程任务,task的ID属性可以用于判断线程启动的先后
	 */
	protected Task lastTask = new Task(0) {

		@Override
		public void run() {

		}
	};

	@AfterViews
	void _init() {
		mContext = getActivity();
		mActivity = getActivity();
		
		mThreadPoolManager = ThreadPoolManager_.getInstance_(getActivity());
		
		mBaiduLocationService = BaiduLocationService_.getInstance_(getActivity().getApplicationContext());
		
		mNetWorkCenter = NetWorkCenter_.getInstance_(getActivity().getApplicationContext());
		
		/*model实例化*/
		mShopsModel = ShopsModel_.getInstance_(getActivity().getApplicationContext());
		mShoppingCarModel = ShoppingCarModel_.getInstance_(getActivity().getApplicationContext());
		mGoodsModel = GoodsModel_.getInstance_(getActivity().getApplicationContext());
		mActivitiesModel = ActivitiesModel_.getInstance_(getActivity().getApplicationContext());
		mFileModel = FileModel_.getInstance_(getActivity().getApplicationContext());
		mPropPanelModel = PropPanelModel_.getInstance_(getActivity().getApplicationContext());
		mOrderModel = OrderModel_.getInstance_(getActivity().getApplicationContext());
		mUserModel = UserModel_.getInstance_(getActivity().getApplicationContext());
		
		progress = getProgressDialog("正在加载,请稍后...");
		progress.setCancelable(false);
		
		obtainInfo();
	}
	
	/**
	 * 接收跳转后要传递的数据bean
	 * @param info
	 */
	@SuppressWarnings("rawtypes")
	public void receiveInfo(FmInfoBean info) {
		this.infoBean = info;
	}
	
	/**
	 * 得到传递的数据bean
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public FmInfoBean getInfoBean(){
		return infoBean;
	}
	
	@Override
	public void onHiddenChanged(boolean hidden) {
		super.onHiddenChanged(hidden);
		if (!hidden) {
			obtainInfo();// 接收数据
		}
	}

	public ProgressDialog getProgressDialog(String msg) {
		ProgressDialog progressDialog = new ProgressDialog(mContext);
		// progressDialog.setIndeterminate(true);
		progressDialog.setMessage(msg);
		progressDialog.setCancelable(true);
		return progressDialog;
	}

	@UiThread
	protected void showToast(final String msg) {
		Toast.makeText(mContext, msg, Toast.LENGTH_SHORT).show();
	}

	/**
	 * UI线程执行一个任务
	 * 
	 * @param run
	 */
	protected void runOnUi(Runnable run) {
		if (mActivity != null) {
			mActivity.runOnUiThread(run);
		}
	}

	/**
	 * 子线程执行一个任务
	 * 
	 * @param task
	 */
	protected void executeTask(Task task) {
		this.lastTask = task;
		mThreadPoolManager.executeTask(task);
	}

	/**
	 * 
	 * @param run
	 */
	protected void executeTask(Runnable run) {
		mThreadPoolManager.executeTask(run);
	}

	/**
	 * 隐藏输入法
	 * 
	 * @param context
	 * @param achor
	 */
	public static void hideSoftInput(Context context, View achor) {
		InputMethodManager imm = (InputMethodManager) context
				.getSystemService(Context.INPUT_METHOD_SERVICE);
		imm.hideSoftInputFromWindow(achor.getWindowToken(),
				InputMethodManager.HIDE_NOT_ALWAYS);
	}

	@Override
	public void onViewCreated(View view,Bundle savedInstanceState) {
		view.setOnTouchListener(this);
		super.onViewCreated(view, savedInstanceState);
	}
	
	/**
	 * UI线程执行一个任务
	 * 
	 * @param run
	 */
	public void runOnUI(Runnable run) {
		getActivity().runOnUiThread(run);
	}
	
	/**
	 * 打开一个Activity
	 * 
	 * @param clazz
	 * @param info
	 */
	@SuppressWarnings("rawtypes")
	public void openActivity(final Class clazz, final Info info) {
		runOnUI(new Runnable() {

			@Override
			public void run() {
				Intent intent = new Intent();
				intent.setClass(mContext, clazz);
				if (info != null) {
					intent.putExtra(INFO_NAME, info);
				}
				startActivity(intent);
			}
		});

	}

	/**
	 * 防止View的事件穿透
	 */
	@Override
	public boolean onTouch(View v, MotionEvent event) {
		return true;
	}

	@UiThread
	void dismissProgressDialog() {
		if (progress != null) {
			progress.dismiss();
		}
	}

	@UiThread
	void showProgressDialog() {
		try {
			if (progress == null) {
				progress = new ProgressDialog(mContext);
				progress.setMessage("正在加载,请稍后...");
				progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
			}
			progress.setCancelable(true);
			progress.show();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@UiThread
	void showLocationDialog() {
		try {
			if (locationProgress == null) {
				locationProgress = new ProgressDialog(mContext);
				locationProgress.setMessage("正在定位,请稍后...");
				locationProgress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
			}
			locationProgress.setCancelable(true);
			locationProgress.show();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	@UiThread
	void dismissLocationDialog() {
		if (locationProgress != null) {
			locationProgress.dismiss();
		}
	}
	/**
	 * 显示动画loading
	 */
	@UiThread
	public void showLoading(ImageView iv) {
		iv.setImageResource(R.anim.loading);
		animationDrawable = (AnimationDrawable) iv.getDrawable();
		animationDrawable.start();
	}

	/**
	 * 隐藏动画loading
	 */
	@UiThread
	public void hideLoading() {
		if (animationDrawable != null && animationDrawable.isRunning()) {
			animationDrawable.stop();
		}
	}

	@Override
	public void onPause() {
		super.onPause();
		mNetWorkCenter.removeRetry();
	}

	/**
	 * 获取数据bean的逻辑统一放在这里
	 */
	protected abstract void obtainInfo();
	
	
}
